package mredis

import (
	"fmt"
	"gitee.com/dennis-mxx/mxx-core/mexception"
	"gitee.com/dennis-mxx/mxx-core/mlogger"
	"gitee.com/dennis-mxx/mxx-core/msystem"
	"github.com/go-redis/redis"
	"time"
)

var RedisClient *redis.Client

func InitRedis() {
	redisOptions := msystem.Redis
	if redisOptions.Addr == "" {
		redisOptions.Addr = "127.0.0.1:6379"
	}
	if redisOptions.Network == "" {
		redisOptions.Network = "tcp"
	}

	RedisClient = redis.NewClient(&redis.Options{
		Network:     redisOptions.Network,
		Addr:        redisOptions.Addr,
		Password:    redisOptions.Password,
		DB:          redisOptions.DB,
		ReadTimeout: time.Duration(redisOptions.ReadTimeout) * time.Second,
		DialTimeout: time.Duration(redisOptions.DialTimeout) * time.Second,
	})
	if RedisClient == nil {
		panic(mexception.NewException(mexception.RedisException, fmt.Sprintf("connection to redis session exception %s:%s [%v]", redisOptions.Network, redisOptions.Addr, redisOptions.DB)))
	} else {
		mlogger.Logger.Info(fmt.Sprintf("connection to redis session {%s:%s [%v]} successful", redisOptions.Network, redisOptions.Addr, redisOptions.DB))
	}

}
